﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Diagnostics;
using System.Text;

using XMK.Tadong.Biz;
using XMK.Framework.Model;
using XMK.Tadong.Model;


namespace XMK.Tadong.Web.Views.DataSearch
{
    public partial class StructDataSearchView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtKeyword.Text))
            {
                divContainer.InnerHtml = "您未输入任何查询条件！";
                return;
            }

            Stopwatch watch = new Stopwatch();

            watch.Start();

            V_Base_Struct_KeywBiz biz = new V_Base_Struct_KeywBiz();
            PageInfo pageInfo = new PageInfo
            {
                CurrentPage = AspNetPager1.CurrentPageIndex,
                PageSize = AspNetPager1.PageSize
            };
            OrderInfo order = new OrderInfo();
            order.ColName = "cnt";
            order.Sort = "desc";
            ResponseInfo resInfo = biz.QueryList(txtKeyword.Text.Trim(),pageInfo, order);
            if (!string.IsNullOrWhiteSpace(resInfo.ErrMsg))
            {
                divContainer.InnerHtml = resInfo.ErrMsg;
                return;
            }

            IEnumerable<V_Base_Struct_KeywModel> xmkFileInfoList = resInfo.Data as IEnumerable<V_Base_Struct_KeywModel>;
            if (xmkFileInfoList == null || xmkFileInfoList.Count() == 0)
            {
                StringBuilder reportBuilder = new StringBuilder();

                reportBuilder.AppendLine("<div id='content_left' >");

                //性能报告
                searchCount.InnerHtml = string.Format("<span>为您找到相关结果{0}个,耗时：{1} 毫秒</span>",
                    0, watch.ElapsedMilliseconds);
                watch.Stop();

                reportBuilder.Append("没有搜索到任何数据！");
                reportBuilder.AppendLine("</div>");

                divContainer.InnerHtml = reportBuilder.ToString();

                return;
            }

            AspNetPager1.RecordCount = (int)resInfo.Page.Records;

            StringBuilder contentBuilder = new StringBuilder();

            contentBuilder.AppendLine("<div id='content_left' >");

            //性能报告
            searchCount.InnerHtml = string.Format("<span>为您找到相关结果{0}个,耗时：{1} 毫秒</span>",
                resInfo.Page.Records, watch.ElapsedMilliseconds);
            watch.Stop();

            foreach (V_Base_Struct_KeywModel model in xmkFileInfoList)
            {
                contentBuilder.AppendLine("<div class='ec_pp_top' >");
                contentBuilder.AppendLine("<div class='ec_title' >");
                contentBuilder.AppendLine("<h3 class='t ec_nowrap' >");
                contentBuilder.AppendFormat("<a id={1} title={0} proc={3} >【{0}】表中共搜索到（{2}）条符合条件的记录 </a></h3></div>",
                    model.Table_CN_Name, model.Table_EN_Name, model.Keyw_Cnt, model.Table_Proc_Name);
                contentBuilder.AppendLine("</div>");
            }

            contentBuilder.AppendLine("</div>");

            divContainer.InnerHtml = contentBuilder.ToString();

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            btnSearch_Click(null, null);
        }
    }
}